package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.more.MoreUiUtil;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityChangeIconBinding;
import flc.ast.utils.Content;
import stark.common.basic.utils.SPUtil;

public class ChangeIconActivity extends BaseAc<ActivityChangeIconBinding> {


    @Override
    protected int onCreate() {
        return R.layout.activity_change_icon;
    }

    @Override
    protected void initView() {
        mDataBinding.ivChangeIconBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivModeOpen.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivModeOpen:
                boolean isOpened = SPUtil.getBoolean(mContext, Content.IS_CALCULATOR_MODE, false);
                SPUtil.putBoolean(mContext, Content.IS_CALCULATOR_MODE, !isOpened);
                if (isOpened) {
                    mDataBinding.ivModeOpen.setImageResource(R.drawable.icon_kg1);
                    setPrimitiveIcon();
                } else {
                    mDataBinding.ivModeOpen.setImageResource(R.drawable.icon_kg1a);
                    setCalculatorIcon();
                }
                break;
        }
    }

    @Override
    protected void initData() {
        boolean isOpened = SPUtil.getBoolean(mContext, Content.IS_CALCULATOR_MODE, false);
        if (isOpened) {
            //将控件开关设为开
            mDataBinding.ivModeOpen.setImageResource(R.drawable.icon_kg1a);
        } else {
            //将控件开关设为关
            mDataBinding.ivModeOpen.setImageResource(R.drawable.icon_kg1);
        }
        ToastUtils.showShort(getString(R.string.tip_text2));
    }

    private void setCalculatorIcon() {
        PackageManager packageManager = getPackageManager();
        packageManager.setComponentEnabledSetting(new ComponentName(this, "flc.ast" +
                ".SplashActivity"), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager
                .DONT_KILL_APP);
        packageManager.setComponentEnabledSetting(new ComponentName(this, "flc.ast" +
                ".CalculatorActivity"), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager
                .DONT_KILL_APP);
    }

    private void setPrimitiveIcon() {
        PackageManager packageManager = getPackageManager();
        packageManager.setComponentEnabledSetting(new ComponentName(this, "flc.ast" +
                        ".CalculatorActivity"), PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
        packageManager.setComponentEnabledSetting(new ComponentName(this, "flc.ast" +
                ".SplashActivity"), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager
                .DONT_KILL_APP);
    }


}